#!/bin/bash
#decsription install zookeeper
SOFTDIR=/usr/local/src
ZKVER=3.4.9
DIR=/opt/data
DATADIR1=${DIR}/zk01
DATADIR2=${DIR}/zk02
DATADIR3=${DIR}/zk03
IP=$(ifconfig eth0|grep inet|grep -v "inet6"|awk '{print $2'})
#DATALOGDIR=/opt/log/
#create zk datadir and touch myid
[ ! -d $DIR ] && mkdir -p $DIR
for dir in zk01 zk02 zk03;do
	[ ! -d ${DIR}/$dir ] && mkdir -p ${DIR}/$dir
done
echo "01" >  ${DATADIR1}/myid
echo "02" >  ${DATADIR2}/myid
echo "03" >  ${DATADIR3}/myid

#install java environment
cd $SOFTDIR && [ ! -f jdk-8u131-linux-x64.rpm ] && wget http://soft.51yuki.cn/jdk-8u131-linux-x64.rpm && yum -y localinstall jdk-8u131-linux-x64.rpm
#install zookeeper
cd $SOFTDIR && [ ! -f zookeeper-${ZKVER}.tar.gz ] && wget http://soft.51yuki.cn/zookeeper-${ZKVER}.tar.gz
cd /usr/local 
if [ ! -d zookeeper-${ZKVER} ];then
	cd $SOFTDIR && tar xf zookeeper-${ZKVER}.tar.gz -C /usr/local
	ln -s /usr/local/zookeeper-${ZKVER} /usr/local/zookeeper
fi
#configure zookeeper env variables
echo "export PATH=/usr/local/zookeeper/bin:$PATH" > /etc/profile.d/zookeeper.sh
source /etc/profile.d/zookeeper.sh

#configure zookeeper

cd /usr/local/zookeeper/conf
cat >zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=$DATADIR1
clientPort=2181
server.01=${IP}:3181:4181
server.02=${IP}:3182:4182
server.03=${IP}:3183:4183
EOF
for cfg in zk01.cfg zk02.cfg zk03.cfg;do
	cp zoo.cfg $cfg
done
sed -i 's/zk01/zk02/g'  /usr/local/zookeeper/conf/zk02.cfg
sed -i 's/2181/2182/g' /usr/local/zookeeper/conf/zk02.cfg
sed -i 's/zk01/zk03/g'  /usr/local/zookeeper/conf/zk03.cfg
sed -i 's/2181/2183/g' /usr/local/zookeeper/conf/zk03.cfg

#start Zookeeper
for cfg in zk01.cfg zk02.cfg zk03.cfg;do
	/usr/local/zookeeper/bin/zkServer.sh start /usr/local/zookeeper/conf/$cfg
done



